<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class RequireController extends AdminbaseController {

    protected $data_model,$requireAns_model;


    public function _initialize() {
        parent::_initialize();
        $this->data_model = D("Require");
        $this->requireAns_model = D("RequireAns");
    }

    public function index1()
    {
        $id=I('type_id/d',0);
        $this->_index($id);
    }

    public function index2()
    {
        $id=I('type_id/d',0);
        $this->_index($id);
    }

    public function index3()
    {
        $id=I('type_id/d',0);
        $this->_index($id);
    }

    /**
     * 社区帖子列表
     */
    private function _index($id){
        $where = array("t.type_id"=>$id);

        $type=$this->_getBBSType($id);

        /**是否加精**/
        $istop=I('request.istop');
        if(is_numeric($istop) && $istop!==""){
            $where['t.istop']=array(
                array('EQ',$istop)
            );
        }

        $start_time=I('request.start_time');
        if(!empty($start_time)){
            $start_time=strtotime($start_time);
            $where['t.create_time']=array(
                array('EGT',$start_time)
            );
        }

        $end_time=I('request.end_time');
        if(!empty($end_time)){
            $end_time=strtotime($end_time);
            if(empty($where['t.create_time'])){
                $where['t.create_time']=array();
            }
            array_push($where['t.create_time'], array('ELT',$end_time));
        }

        $keyword=I('request.keyword');
        if(!empty($keyword)){
            $keywordSearch['t.title'] = array('like',"%$keyword%");
            $keywordSearch['t.content'] = array('like',"%$keyword%");
            $keywordSearch['_logic'] = 'or';
            $where['_complex'] = $keywordSearch;
        }
        //dump($where);exit;

        $count=$this->data_model->alias("t")->where($where)->count();
        $page = $this->page($count, 20);

        $data=$this->data_model
            ->alias("t")
            ->where($where)
            ->field("t.id,t.title,LEFT(t.content,60) AS content,t.create_time,t.status")
            ->group("t.id")
            ->order("t.id DESC")
            ->limit($page->firstRow, $page->listRows)
            //->fetchSql(true)
            ->select();
        //dump($data);exit;
        //
        foreach ($data as &$value) {
            $value['content']=htmlspecialchars($value['content']);
        }


        $this->assign("formget",I("request."));
        $this->assign("page", $page->show('Admin'));
        $this->assign('data',$data);
        $this->assign('type',$type);
        $this->display("index");
    }

    public function pageShow(){
        $id=I('get.id',0,'intval');
        $where=array();
        $where['t.id'] = array('eq',$id);
        //$where['status'] = array('eq',1);
        
        list($join,$field)=R("Api/Require/getInfoData");
        //dump($join);dump($field);
        $field.=",t.type_id";

        $content = $this->data_model
                    ->alias("t")
                    ->join($join)/*帖子图片*/
                    ->where($where)
                    ->field($field)
                    ->group("t.id")
                    ->find();

        $content['type']=$this->_getBBSType($content['type_id']);
        //dump($content);exit;

        if(empty($content)){
            header('HTTP/1.1 404 Not Found');
            header('Status:404 Not Found');
            if(sp_template_file_exists(MODULE_NAME."/404")){
                $this->display(":404");
            }
            return ;
        }

        $this->assign($content);
        $this->display();
    }

    /**
     * 投诉
     */
    public function complain(){
        $where = array();
        $where['a.complain']=array('neq','');
        /**搜索条件**/

        $type=I('request.type_id');
        if(!empty($type)){
            $where['t.type_id'] = $type;
        }

        list($join,$field)=R("Api/Require/getInfoData");
        //dump($where);

        $count==$this->data_model
                    ->alias("t")
                    ->join($join)
                    ->where($where)
                    ->group("t.id")
                    ->count();

        $page = $this->page($count, 20);

        $data=$this->data_model
                    ->alias("t")
                    ->join($join)
                    ->where($where)
                    ->field($field)
                    ->group("t.id")
                    ->order("t.id DESC")
                    ->limit($page->firstRow, $page->listRows)
                    //->fetchSql(true)
                    ->select();
        

        foreach ($data as &$value){
            $value['images']=analysisImages($value['images']);
        }
        unset($value);

        //dump($data);//exit;
        
        $types=$this->_getBBSType();

        $this->assign("formget",I("request."));
        $this->assign("page", $page->show('Admin'));
        $this->assign('data',$data);
        $this->assign('types',$types);
        $this->display();
    }

    public function complainDetail(){
        $id=I('get.id',0,'intval');
        $where = array();
        /**搜索条件**/


        $where['t.id'] = array('eq',$id);

        list($join,$field)=R("Api/Require/getInfoData");

        $data=$this->data_model
                    ->alias("t")
                    ->join($join)
                    ->where($where)
                    ->field($field)
                    ->group("t.id")
                    //->fetchSql(true)
                    ->find();
        

        $data['images']=analysisImages($data['images']);
        //dump($data);exit;

        if(empty($data)){
            header('HTTP/1.1 404 Not Found');
            header('Status:404 Not Found');
            if(sp_template_file_exists(MODULE_NAME."/404")){
                $this->display(":404");
            }
            return ;
        }

        $this->assign($data);
        $this->display();
    }

    public function complainStatus(){
        $ids = I('ids');
        $status=I('request.status');
        if (empty($ids) || !is_numeric($status)){
            $this->error("参数错误!");
        }
        $statusInfo=$this->_statusinfo();
        if (parent::_setStatus($this->requireAns_model,$ids,$status,"is_resolve")!==false) {
            $this->success("成功");
        } else {
            $this->error("失败");
        }
    }


    /**
     * 排序
     */
    public function listorders() {
        $status = parent::_listorders($this->data_model);
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }

    // 删除
    public function delete(){
        $ids = I("ids");
        $status = parent::_delete($this->data_model,$ids);

        if ($status) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }


    public function status(){
        $ids = I('ids');
        $status=I('request.status');
        if (empty($ids) || !is_numeric($status)){
            $this->error("参数错误!");
        }
        $statusInfo=$this->_statusinfo();
        if (parent::_setStatus($this->data_model,$ids,$status)!==false) {
            $this->success($statusInfo[$status]['success']);
        } else {
            $this->error($statusInfo[$status]['error']);
        }
    }

    private function _getBBSType($id=0)
    {
        $data=[
            1=>["id"=>1,"name"=>"用户需求"],
            2=>["id"=>2,"name"=>"商家招聘"],
            3=>["id"=>3,"name"=>"技师求职"]
        ];
        if (!empty($id)) {
            $data=$data[$id];
        }
        return $data;
    }

}